/**
 * MVT工具
 */
class MVTService {
  viewer: any
  constructor(viewer: any) {
    this.viewer = viewer;
  }
  init() {
    // 在SuperMap桌面产品中将地图生成好的矢量瓦片地图通过SuperMap iServer发布成矢量瓦片或三维服务后， 在WebGL中利用接口addVectorTilesMap将MVT图层服务添加到场景中
    const mvtMap = this.viewer.scene.addVectorTilesMap({
      url: 'http://{s}/realspace/services/map-mvt-JingJinDiQuDiTu/restjsr/v1/vectortile/maps/%E4%BA%AC%E6%B4%A5%E5%9C%B0%E5%8C%BA%E5%9C%B0%E5%9B%BE',
      subdomains: ['www.supermapol.com'], // 支持多个子域
      canvasWidth: 512,
      name: 'testMVT',
      viewer: this.viewer,
    })
    const layerReadyPromise = mvtMap.readyPromise
    Cesium.when(layerReadyPromise, (data) => {
      const bounds = mvtMap.rectangle
      this.viewer.scene.camera.setView({ // 定位到指定的视角
        destination: new Cesium.Cartesian3.fromRadians(
          (bounds.east + bounds.west) * 0.5,
          (bounds.north + bounds.south) * 0.5,
          10000
        ),
        orientation: {
          heading: 0,
          roll: 0,
        },
      })

      const mapboxStyle = mvtMap.mapboxStyle
      for (let id in mapboxStyle.layers) {
        const layer = mapboxStyle.layers[id]
        if (layer.type === 'symbol') {
          mvtMap.setLayoutProperty(layer.id, 'text-max-width', 2) // 图层文本一行最多显示两个字，多了换行显示
        }
      }
    })
  }
}

export default MVTService